<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/10/11 16:17
 * Modified Time: 2019/10/11 16:17
 * Version: 2.0
 */

namespace App\Modules\Store\Repositories;


use App\Models\RenewalSetting;
use Flower\Core\Repository\Eloquent\BaseRepository;

class RenewalSettingRepository extends BaseRepository
{
    public function model()
    {
        return RenewalSetting::class;
    }

    public function getRenewalSetting($user_id,$store_id)
    {
        return $this->model
            ->select('renewal.rule')
            ->leftJoin('renewal','renewal_setting.renewal_id','renewal.id')
            ->where('renewal_setting.user_id',$user_id)
            ->where('renewal_setting.store_id',$store_id)
            ->first();
    }

    /**
     * 获取剩余服务天数
     * @param $user_id
     * @param $store_id
     * @return mixed
     */
    public function getServiceRemainingDays($user_id,$store_id)
    {
        $select = [
            'hx_renewal_setting.charge_end',
            'DATEDIFF(NOW(),hx_renewal_setting.charge_begin) as service_days',
            'IF(DATEDIFF(hx_renewal_setting.charge_end,NOW())>0,0,1) as has_expired'
        ];
        $select = implode($select,',');
        return $this->model
            ->selectRaw($select)
            ->leftJoin('renewal','renewal_setting.renewal_id','renewal.id')
            ->where('renewal_setting.user_id',$user_id)
            ->where('renewal_setting.store_id',$store_id)
            ->first();
    }

}